package org.come.Jpanel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;

import com.tool.image.ImageMixDeal;
import com.tool.tcp.Sprite;
import com.tool.tcp.SpriteFactory;
import org.come.until.CutButtonImage;
import org.come.until.GoodsListFromServerUntil;
import org.come.until.ScrollUI;
import org.come.until.SrcollPanelUI;

import com.tool.ModerateTask.TaskRoleData;
import com.tool.btn.DreamlandTrialBtn;
import com.tool.btn.FormsOnOffBtn;
import com.tool.tcpimg.RichLabel;
import com.tool.tcpimg.UIUtils;
import com.updateNew.MyIsif;

//import static org.come.test.Main.imageMap;

/**
 * @author HGC<br>
 * @time 2020年5月11日 下午1:35:01<br>
 * @class 类名:DreamlandTrialMainJpanel 幻境试炼面板 编号:111 <br>
 */
public class DreamlandTrialMainJpanel extends JPanel {


    private int pageNow =-1,pageMax,nowLvl,chooseNum = -1,moveNum = -1;
    private JLabel labPage,labLvl,labNum;
    private DreamlandTrialModelJpanel[] modelJpanelList;
    private DreamlandTrialBtn btnHomePage, btnUpPage, btnLastPage,btnDownPage,btnChallenge,btnRecruit,btnAdd;
    private JScrollPane jscPanel;
    private RichLabel richText;

    static Sprite tcp = SpriteFactory.VloadSprite("resource/FightingSkill/持续状态/tx1.tcp", null);//特效路径
    static Sprite tcp2 = SpriteFactory.VloadSprite("resource/FightingSkill/持续状态/tx1.tcp", null);//特效路径
    private ImageIcon icon = null;
    static {
        if (tcp == null) {
            tcp = tcp;

        }
    }
    static {
        if (tcp2 == null) {
            tcp2 = tcp2;

        }
    }


    public DreamlandTrialMainJpanel() {
        setPreferredSize(new Dimension(604,383));
        setOpaque(false);
        setLayout(null);
        

        // 关闭按钮事件
//            FormsOnOffBtn offBtn = new FormsOnOffBtn("skin/8100226/button/1.png", 1, 111);
//            offBtn.setBounds(472 - 37, 10, 25, 25);
//            this.add(offBtn);


        getBtnAdd();
        getBtnChallenge();
        getBtnDownPage();
        getBtnHomePage();
        getBtnLastPage();
        getBtnUpPage();
        getLabPage();
        getLabNum();
        getLabLvl();
        getModelJpanelList();
        getJscPanel();
        showLvlTier(1);
    }

    /**最高奖励展示*/
    public void showView(String path){
        iconAward = GoodsListFromServerUntil.imgpathAdaptive(path,-1,-1);
        if (pageNow==-1) {
            showLvlTier(null);
        }else{
            showLvlTier(pageNow);
        }
    }

    /**刷新关卡*/
    public void showLvlTier(Integer num){
        //获取挑战记录
        int sumReceive = TaskRoleData.SumReceive(6,3);
        if(sumReceive == 0){
            pageNow = pageMax = 1;
        }else{
            pageMax = sumReceive / 6+1;
            nowLvl = sumReceive % 6;
            if(num==null){
                pageNow = pageMax;
                chooseNum = -1;
            }else{
                pageNow = num;
            }
        }
        labNum.setText(String.valueOf(TaskRoleData.SumReceive(6,4)));
        labPage.setText(pageNow+"/"+pageMax);
        labLvl.setText(String.valueOf(pageNow));
        for (int i = 0,len = modelJpanelList.length; i < len; i++) {
            if(pageNow==pageMax){
                if(i<nowLvl){//已通过   消耗次数1
                    modelJpanelList[i].changeShow(0);
                }else if(i==nowLvl){//可挑战   消耗次数0
                    modelJpanelList[i].changeShow(1);
                }else{//未解锁
                    modelJpanelList[i].changeShow(2);
                }
            }else{
                modelJpanelList[i].changeShow(0);
            }
        }
        if(pageNow==pageMax){
            chooseNum = nowLvl;
        }else{
            chooseNum = 5;
        }
        for (int i=0;i<=modelJpanelList.length-1;i++){
            modelJpanelList[i].repaint();
        }
    }

    private ImageIcon iconChoose;
    private ImageIcon iconAward;

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if(icon == null){
            icon = new ImageIcon( "inkImg/newbackground/B300A.png");
        }
        g.drawImage(icon.getImage(), 1, 15, 604, 383, this);
        if(iconAward!=null)
            g.drawImage(iconAward.getImage(), 47, 44, null);


    }
    @Override
    protected void paintChildren(Graphics g) {
        super.paintChildren(g);
        if(iconChoose == null){
            iconChoose = CutButtonImage.getImage("inkImg/background/S202.png", -1, -1);
        }
        for (int i = 0; i < 6; i++) {
            if(chooseNum == i||moveNum == i){
                g.drawImage(iconChoose.getImage(), 375, 44+i*49, null);
            }
        }

    }

    public int getPageNow() {
        return pageNow;
    }

    public void setPageNow(int pageNow) {
        this.pageNow = pageNow;
    }

    public int getPageMax() {
        return pageMax;
    }

    public void setPageMax(int pageMax) {
        this.pageMax = pageMax;
    }


    public JLabel getLabLvl() {
        if(labLvl == null){
            labLvl = new JLabel("100",SwingConstants.CENTER);
            labLvl.setForeground(Color.yellow);
            labLvl.setFont(UIUtils.TEXT_FONT1);
            labLvl.setBounds(515,25,36,14);
            this.add(labLvl);
        }
        return labLvl;
    }

    public void setLabLvl(JLabel labLvl) {
        this.labLvl = labLvl;
    }


    //    开始
    public DreamlandTrialBtn getBtnChallenge() {
        if(btnChallenge == null){
            btnChallenge = new DreamlandTrialBtn("skin/8100226/newbutton/B35.png", 1, UIUtils.COLOR_BTNTEXT, UIUtils.TEXT_FONT, "", 4, this);
            btnChallenge.setBounds(122,283, 60, 60);
            this.add(btnChallenge);
        }
        return btnChallenge;
    }

    public void setBtnChallenge(DreamlandTrialBtn btnChallenge) {
        this.btnChallenge = btnChallenge;
    }

    public DreamlandTrialBtn getBtnAdd() {
        if(btnAdd == null){
            btnAdd = new DreamlandTrialBtn(( "skin/8100226/newbutton/B34.png"), 1, UIUtils.COLOR_BTNTEXT, UIUtils.TEXT_FONT, "购买次数", 6, this);
            btnAdd.setBounds(245, 310, 68, 17);
            this.add(btnAdd);
        }
        return btnAdd;
    }

    public void setBtnAdd(DreamlandTrialBtn btnAdd) {
        this.btnAdd = btnAdd;
    }

    public ImageIcon getIcon() {
        return icon;
    }

    public void setIcon(ImageIcon icon) {
        this.icon = icon;
    }






    public DreamlandTrialBtn getBtnHomePage() {
        if(btnHomePage == null){
            btnHomePage = new DreamlandTrialBtn("skin/8100226/button/2.png", 1, UIUtils.COLOR_BTNTEXT, UIUtils.TEXT_FONT, "首页", 0, this);
            btnHomePage.setBounds(420, 359, 34, 17);
            this.add(btnHomePage);
        }
        return btnHomePage;
    }


    public void setBtnHomePage(DreamlandTrialBtn btnHomePage) {
        this.btnHomePage = btnHomePage;
    }

    public DreamlandTrialBtn getBtnUpPage() {
        if(btnUpPage == null){
            btnUpPage = new DreamlandTrialBtn("skin/8100226/button/10.png", 1, 1, this);
            btnUpPage.setBounds(456, 359, 18, 18);
            this.add(btnUpPage);
        }
        return btnUpPage;
    }

    public void setBtnUpPage(DreamlandTrialBtn btnUpPage) {
        this.btnUpPage = btnUpPage;
    }



    public JLabel getLabPage() {
        if(labPage == null){
            labPage = new JLabel("1/20",SwingConstants.CENTER);
            labPage.setForeground(Color.white);
            labPage.setFont(UIUtils.TEXT_FONT1);
            labPage.setBounds(475, 359, 42, 16);
            this.add(labPage);
        }
        return labPage;
    }

    public void setLabPage(JLabel labPage) {
        this.labPage = labPage;
    }



    public DreamlandTrialBtn getBtnDownPage() {
        if(btnDownPage == null){
            btnDownPage = new DreamlandTrialBtn("skin/8100226/button/9.png", 1, 2, this);
            btnDownPage.setBounds(522, 359, 18, 18);
            this.add(btnDownPage);
        }
        return btnDownPage;
    }

    public void setBtnDownPage(DreamlandTrialBtn btnDownPage) {
        this.btnDownPage = btnDownPage;
    }




    public DreamlandTrialBtn getBtnLastPage() {
        if(btnLastPage == null){
            btnLastPage = new DreamlandTrialBtn("skin/8100226/button/2.png", 1, UIUtils.COLOR_BTNTEXT, UIUtils.TEXT_FONT, "末页", 3, this);
            btnLastPage.setBounds(542, 359, 34, 17);
            this.add(btnLastPage);
        }
        return btnLastPage;
    }

    public void setBtnLastPage(DreamlandTrialBtn btnLastPage) {
        this.btnLastPage = btnLastPage;
    }




    class MListener implements MouseListener{

        private int num;

        public MListener(int num) {
            this.num = num;
        }

        @Override
        public void mouseClicked(MouseEvent e) {

        }

        @Override
        public void mousePressed(MouseEvent e) {
            if(modelJpanelList[num].getType()!= 2){
                chooseNum = num;
            }
        }

        @Override
        public void mouseReleased(MouseEvent e) {

        }

        @Override
        public void mouseEntered(MouseEvent e) {
            if(modelJpanelList[num].getType()!= 2){
                moveNum = num;
            }
        }

        @Override
        public void mouseExited(MouseEvent e) {
            if(modelJpanelList[num].getType()!= 2){
                moveNum = - 1;
            }
        }

    }
    public DreamlandTrialModelJpanel[] getModelJpanelList() {
        if(modelJpanelList == null){
            modelJpanelList = new DreamlandTrialModelJpanel[6];
            for (int i = 0; i < modelJpanelList.length; i++) {
                modelJpanelList[i] = new DreamlandTrialModelJpanel();
                modelJpanelList[i].setBounds(375, 44+i*49, 227, 47);
//                    modelJpanelList[i].setBackground(Color.red);
                modelJpanelList[i].addMouseListener(new MListener(i));
                this.add(modelJpanelList[i]);
            }
        }
        return modelJpanelList;
    }

    public void setModelJpanelList(DreamlandTrialModelJpanel[] modelJpanelList) {
        this.modelJpanelList = modelJpanelList;
    }

    public JLabel getLabNum() {
        if(labNum == null){
            labNum = new JLabel("",SwingConstants.CENTER);
            labNum.setBounds(263, 250, 30, 14);
            labNum.setForeground(Color.yellow);
            labNum.setFont(UIUtils.TEXT_FONT61);
            this.add(labNum);
        }

        return labNum;
    }

    public void setLabNum(JLabel labNum) {
        this.labNum = labNum;
    }

    public JScrollPane getJscPanel() {
        if (jscPanel == null) {
            // 属性滚动条
            jscPanel = new JScrollPane(getRichText());
            jscPanel.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
            jscPanel.getVerticalScrollBar().setUI(new SrcollPanelUI());
            jscPanel.getVerticalScrollBar().setUnitIncrement(20);
            jscPanel.getViewport().setOpaque(false);
            jscPanel.setOpaque(false);
            jscPanel.setBounds(202, 44, 164, 159);
            jscPanel.setBorder(BorderFactory.createEmptyBorder());
            jscPanel.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
            this.add(jscPanel);
        }

        return jscPanel;
    }

    public void setJscPanel(JScrollPane jscPanel) {
        this.jscPanel = jscPanel;
    }

    public RichLabel getRichText() {
        if(richText == null){
            richText = new RichLabel();
            richText.setText("试炼规则：#r1,累计挑战奖励机制#r2,原则可以无限挑战#r3,成功挑战后解锁新关卡#r4,挑战新关卡不消耗挑战次数#r5,每层的奖励唯一，可以选择性击杀#r6,每次击杀胜利再进入界面可能没有刷新通关,此时你可以点击购买次数刷新界面,无须真正购买次数");
            Dimension d = richText.computeSize(151);
            richText.setSize(d);
            richText.setPreferredSize(d);
        }
        return richText;
    }

    public void setRichText(RichLabel richText) {
        this.richText = richText;
    }

    public int getChooseNum() {
        return chooseNum;
    }

    public void setChooseNum(int chooseNum) {
        this.chooseNum = chooseNum;
    }

    public int getNowLvl() {
        return nowLvl;
    }

    public void setNowLvl(int nowLvl) {
        this.nowLvl = nowLvl;
    }

    public int getMoveNum() {
        return moveNum;
    }

    public void setMoveNum(int moveNum) {
        this.moveNum = moveNum;
    }

    public ImageIcon getIconChoose() {
        return iconChoose;
    }

    public void setIconChoose(ImageIcon iconChoose) {
        this.iconChoose = iconChoose;
    }

    //新增加tcp
    public void paint(Graphics g) {

        tcp.updateToTime(ImageMixDeal.userimg.getTime(), 0);
//        tcp2.updateToTime(ImageMixDeal.userimg.getTime(), 0);
        super.paint(g);
        tcp.draw(g, 150, 315);
//        tcp2.draw(g, 0, 0);

    }
}
